#include "DIALOG.h"
#include "MC_Include.h"

#define ID_FRAMEWIN_3  (GUI_ID_USER + 0x300)
#define ID_FLISTBOX_0  (GUI_ID_USER + 0x301)
#define ID_FBUTTON_0  (GUI_ID_USER + 0x302)
#define ID_FBUTTON_1  (GUI_ID_USER + 0x303)
#define ID_FBUTTON_2     (GUI_ID_USER + 0x304)

static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] =
{
    { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_3, 225, 30, 400, 240, 0, 0x0, 0},
    { LISTBOX_CreateIndirect, "Listbox", ID_FLISTBOX_0, 9, 7, 211, 187, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Enter", ID_FBUTTON_0, 260, 25, 80, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "Delete", ID_FBUTTON_1, 260, 100, 80, 40, 0, 0x0, 0 },
 //   { BUTTON_CreateIndirect, "Refresh", ID_FBUTTON_2, 260, 130, 80, 30, 0, 0x0, 0 },
};

char celfile_text_buffer[11] = {0};
extern uint8_t open_edit_flag;

FRESULT scan_files(char *path,WM_HWIN hitem)
{
    FRESULT res; 		//部分在递归过程被修改的变量，不用全局变量
    FILINFO fno;
    DIR dir;
    int i;
    char *fn;        // 文件名
	
    #if _USE_LFN
    /* 长文件名支持 */
    /* 简体中文需要2个字节保存一个“字”*/
    static char lfn[_MAX_LFN * 2 + 1];
    fno.lfname = lfn;
    fno.lfsize = sizeof(lfn);
    #endif
    //打开目录
    res = f_opendir(&dir, path);
	//memset(file_name,0,sizeof(file_name));
    if (res == FR_OK)
    {
        i = strlen(path);

        for (;;)
        {
            //读取目录下的内容，再读会自动读下一个文件
            res = f_readdir(&dir, &fno);

            //为空时表示所有项目读取完毕，跳出
            if (res != FR_OK || fno.fname[0] == 0) break;

            #if _USE_LFN
            fn = *fno.lfname ? fno.lfname : fno.fname;
            #else
            fn = fno.fname;
            #endif

            //点表示当前目录，跳过
            if (*fn == '.') continue;

            //目录，递归读取
            if (fno.fattrib & AM_DIR)
            {
                //合成完整目录名
                sprintf(&path[i], "/%s", fn);
                //递归遍历
                res = scan_files(path,hitem);
                path[i] = 0;

                //打开失败，跳出循环
                if (res != FR_OK)
				{
					break;
				}
                   
            }
            else
            {
                printf("%s/%s\r\n", path, fn);				//输出文件名
                /* 可以在这里提取特定格式的文件路径 */
				LISTBOX_AddString(hitem,fn);
				
            }//else
        } //for
    }

    return res;
}

static void _cbDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    int NCode;
    int Id;

    switch (pMsg->MsgId)
    {
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Framewin'
            //
            hItem = pMsg->hWin;
            FRAMEWIN_SetSkinClassic(hItem);
            FRAMEWIN_SetTitleHeight(hItem, 32);
            FRAMEWIN_SetFont(hItem, GUI_FONT_20B_1);
            FRAMEWIN_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            FRAMEWIN_SetText(hItem, "File");
            FRAMEWIN_AddCloseButton(hItem, FRAMEWIN_BUTTON_RIGHT, 0);
            FRAMEWIN_SetClientColor(hItem, GUI_MAKE_COLOR(0x00E7E7E7));
			
			hItem = WM_GetDialogItem(pMsg->hWin, ID_FLISTBOX_0);
			LISTBOX_SetAutoScrollV(hItem,1);
			LISTBOX_SetFont(hItem,GUI_FONT_20_1);
			scan_files("1:/user",hItem);
			
            break;

        case WM_DELETE:

            break;

        case WM_NOTIFY_PARENT:
            Id = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            switch(Id)
            {
                case ID_FLISTBOX_0:
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            break;

                        case WM_NOTIFICATION_SEL_CHANGED:
                            break;
                    }

                    break;

                case ID_FBUTTON_0: // Notifications sent by 'Enter'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            break;

                        case WM_NOTIFICATION_RELEASED:
							hItem = WM_GetDialogItem(pMsg->hWin, ID_FLISTBOX_0);
							LISTBOX_GetItemText(hItem,LISTBOX_GetSel(hItem),celfile_text_buffer,10);
							open_edit_flag = 1;
							//printf("cel %s",celfile_text_buffer);
							GUI_EndDialog(pMsg->hWin, 0);
                            break;
                    }

                    break;

                case ID_FBUTTON_1: // Notifications sent by 'Delete'
                    switch(NCode)
                    {
						char temp_path[20];
                        case WM_NOTIFICATION_CLICKED:
                            break;

                        case WM_NOTIFICATION_RELEASED:
							hItem = WM_GetDialogItem(pMsg->hWin, ID_FLISTBOX_0);
							LISTBOX_GetItemText(hItem,LISTBOX_GetSel(hItem),celfile_text_buffer,10);
							sprintf(temp_path,"1:/user/%s",celfile_text_buffer);
							f_unlink(temp_path);
							LISTBOX_DeleteItem(hItem,LISTBOX_GetSel(hItem));
							memset(celfile_text_buffer,0,sizeof(celfile_text_buffer));
                            break;
                    }

                    break;
					
//				case ID_FBUTTON_2: // Notifications sent by 'Refresh'
//					switch(NCode)
//					{
//						case WM_NOTIFICATION_CLICKED:
//							// USER START (Optionally insert code for reacting on notification message)
//							// USER END
//							break;

//						case WM_NOTIFICATION_RELEASED:
//							hItem = WM_GetDialogItem(pMsg->hWin, ID_FLISTBOX_0);
//							for(char i = 0;i < LISTBOX_GetSel(hItem);i ++)
//								LISTBOX_DeleteItem(hItem,i);
//							scan_files("1:/user",hItem);
//							break;
//							// USER START (Optionally insert additional code for further notification handling)
//							// USER END
//					}
//					break;

            }

            break;

        default:
            WM_DefaultProc(pMsg);
            break;
    }
}


WM_HWIN CreateFilewin(WM_HWIN himt);
WM_HWIN CreateFilewin(WM_HWIN himt)
{
    WM_HWIN hWin;

    hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, himt, 0, 0);
    return hWin;
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
